<?php

namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\admin\validate\AdminUser as AdminUserValidate;
class Login extends AdminBase
{

    public function initialize()
    {
        if($this->isLogin()){
            return $this->redirect(url("index/index"));
        }
    }

    public function index()
    {
        return View::fetch();
    }


    public function check()
    {
        if(!$this->request->isPost()){
            return show(config("status.error"),"请求方式错误");
        }

        $username = $this->request->param("username","","trim");
        $password = $this->request->param("password","","trim");
        $captcha = $this->request->param("captcha","","trim");
        $validate = new AdminUserValidate();
        $data = [
            'username' => $username,
            'password' => $password,
            'captcha' => $captcha
        ];
        if(!$validate->check($data)){
            return show(config("status.error"),$validate->getError());
        }
        try {
            $adminUserObj = new \app\admin\business\AdminUser();
            $result = $adminUserObj->login($data);
        }catch (\Exception $e){
            return show(config("status.error"),$e->getMessage());
        }

        if($result){
            return show(config("status.success"),"登录成功");
        }

        return show(config("status.error"),"登录失败");

    }


    public function md5()
    {
        halt(session(config("admin.session_admin")));
        echo md5("admin_singwa_abc");
    }
}